#include "alertnotice.h"
#include "ui_alertnotice.h"
#include <QSqlQuery>
#include <QMessageBox>
#include <QDialog>
#include <QVBoxLayout>
#include <QLabel>
#include <QTextEdit>
#include <QDialogButtonBox>

AlertNotice::AlertNotice(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::AlertNotice)
{
    ui->setupUi(this);
    QFile mainFile(":/qss/AlertManager.qss");
    if (mainFile.open(QFile::ReadOnly)) {
        QString styleSheet = QLatin1String(mainFile.readAll());
        this->setStyleSheet(styleSheet);
        qDebug() <<"AlertManager QSS打开成功";
    } else {
        qDebug() <<"AlertManager QSS打开失败";
    }
    ui->emailLineEdit->setPlaceholderText("请输入邮箱");
    ui->phoneLineEdit->setPlaceholderText("请输入手机号码");
    setupModel();
    setupConnections();
}

AlertNotice::~AlertNotice()
{
    delete ui;
}

void AlertNotice::onSaveBtnClick()
{
    Logger().logInfo("保存");
    QString email = ui->emailLineEdit->text();
    QString phone = ui->phoneLineEdit->text();

    // 检查是否输入了邮件或电话
    if (email.isEmpty() && phone.isEmpty()) {
        QMessageBox::warning(this, "输入错误", "请输入邮箱或手机号码！");
        return;
    }

    ui->emailLineEdit->clear();
    ui->phoneLineEdit->clear();

    db = SQLiteDatabase::getInstance();
    QSqlQuery sqlQuery(db->getConnection());
    sqlQuery.prepare("UPDATE notice_settings SET email = ?, phone = ? WHERE id = 1");
    sqlQuery.addBindValue(email);
    sqlQuery.addBindValue(phone);

    if (!sqlQuery.exec()) {
        QMessageBox::critical(this, "错误", "保存失败");
    } else {
        QMessageBox::information(this, "成功", "保存成功");
    }
}

void AlertNotice::onRefreshBtnClick()
{
    Logger().logInfo("刷新");
    model->select();
    ui->showNoticeTableView->setModel(model);
}

void AlertNotice::onTableDoubleClicked(const QModelIndex &index)
{
    if (!index.isValid())
        return;

    QString timestamp = model->data(model->index(index.row(), 2)).toString();
    QString content = model->data(model->index(index.row(), 3)).toString();

    QDialog dialog(this);
    dialog.setWindowTitle("通知内容详情");

    QVBoxLayout layout(&dialog);

    QLabel *timestampLabel = new QLabel("时间戳: " + timestamp, &dialog);
    QLabel *contentLabel = new QLabel("通知内容:", &dialog);
    QTextEdit *contentEdit = new QTextEdit(&dialog);
    contentEdit->setPlainText(content);
    contentEdit->setReadOnly(true);

    layout.addWidget(timestampLabel);
    layout.addWidget(contentLabel);
    layout.addWidget(contentEdit);

    QDialogButtonBox buttonBox(QDialogButtonBox::Ok, &dialog);
    layout.addWidget(&buttonBox);

    connect(&buttonBox, &QDialogButtonBox::accepted, &dialog, &QDialog::accept);

    dialog.exec();
}

void AlertNotice::setupModel()
{
    SQLiteDatabase* db = SQLiteDatabase::getInstance();
    model = new QSqlTableModel(this, db->getConnection());
    model->setTable("notice_history");
    model->select();

    // 设置自定义列标题
    model->setHeaderData(0, Qt::Horizontal, tr("记录ID"));
    model->setHeaderData(1, Qt::Horizontal, tr("通知方式"));
    model->setHeaderData(2, Qt::Horizontal, tr("时间戳"));
    model->setHeaderData(3, Qt::Horizontal, tr("通知内容"));

    ui->showNoticeTableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch); //自适应
    ui->showNoticeTableView->setModel(model);

}

void AlertNotice::setupConnections()
{
    connect(ui->saveBtn, &QAbstractButton::clicked, this, &AlertNotice::onSaveBtnClick);
    connect(ui->refreshBtn, &QAbstractButton::clicked, this, &AlertNotice::onRefreshBtnClick);
    connect(ui->showNoticeTableView, &QTableView::doubleClicked, this, &AlertNotice::onTableDoubleClicked); // 连接双击信号
}
